/**
 * Created by wenchuan.li 2018/5/5.
 */
define(function () {

    var self,
        searchData = {
            keyword: ''
        },
        others = {
            ischecked: '0',
            inx: 0
        }
    ;

    var Index = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _tag: 'p_yxsjzpindex', // key, 对象放进pageModule
                _sliderId: 'slider', // tabs
                _refreshAutoInit: true //自动加载刷新控件
            }, win);
        },
        toDelete: function (item) {
            var subdata = {
                orders: []
            };

            var _items;
            if (!item) {
                _items = self._getRefreshObj().data.filterKV({
                    ischecked: '1'
                })
            } else {
                _items = [item];
            }
            if (_items.length == 0) {
                return;
            }

            _items.forEach(function (v) {
                var no = v.GWCMDOCNO;
                if (v.details) {
                    v.details.forEach(function (p1) {
                        subdata.orders.push({
                            GWCMDOCNO: no,
                            GWCMSEQ: p1.GWCMSEQ
                        });
                    });
                }
            });

            app.ajax({
                xurl: "api/wx/gwat200/toDelete",
                loading: true,
                errmsg: '作废操作异常', // 提交失败提示信息
                xdata: {
                    obj: app.jsonString(subdata)
                },
                confirm: {
                    msg: '是否执行作废操作？'
                },
                before: function () { // 判断是否符合提交条件
                    var b = true;

                    return b;
                },
                cb: function (result) {
                    if (result.ok) {
                        self._refresh();
                    }
                }
            });
        },
        toCancel: function (item) {
            var subdata = {
                orders: []
            };

            var _items;
            if (!item) {
                _items = self._getRefreshObj().data.filterKV({
                    ischecked: '1'
                })
            } else {
                _items = [item];
            }
            if (_items.length == 0) {
                return;
            }

            _items.forEach(function (v) {
                var no = v.GWCODOCNO;
                if (v.details) {
                    v.details.forEach(function (p1) {
                        subdata.orders.push({
                            GWCMDOCNO: p1.GWCO030,
                            GWCMSEQ: p1.GWCO031,
                            GWCODOCNO: no
                        });
                    });
                }
            });

            app.ajax({
                xurl: "api/wx/gwat200/cancelZpOrder",
                loading: true,
                errmsg: '取消指派操作异常', // 提交失败提示信息
                xdata: {
                    obj: app.jsonString(subdata)
                },
                confirm: {
                    msg: '是否执行取消指派操作？'
                },
                cb: function (result) {
                    if (result.ok) {
                        var _data = result.data.data;
                        if (_data.length > 0) {
                            app.alert({
                                msg: '不可取消单：' + _data
                            });
                        }
                        self._refresh();
                    }
                }
            });
        },
        toSubmit: function (item) {
            var _items;
            if (!item) {
                _items = self._getRefreshObj().data.filterKV({
                    ischecked: '1'
                })
            } else {
                _items = [item];
            }
            if (_items.length == 0) {
                return;
            }

            app.openPage({
                src: "yxsjzp/order-submit.html",
                argsKey: 'zpordersubmit_page',
                args: {
                    items: _items,
                    cb: function () {
                        self._refresh();
                        self._refresh(1);
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll1', 'scroll2'];
                app.extend(self._refreshVueOpts,{
                    updated: function () {
                        //$(".mui-slider-child").slider();
                        mui('.mui-scroll-wrapper .child').scroll({
                            deceleration: 0.0005,
                            scrollY: false, //是否竖向滚动
                            scrollX: true, //是否横向滚动
                        });
                    }
                });

            }
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat200/getGwcmPage",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    status: goods.type.replace('scroll', ''),
                    keyword: searchData.keyword
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;

                        _super({
                            obj: goods,
                            data: data.addMap({
                                status: result.xdata.status,
                                ischecked: '0'
                            }),
                            nodata: self._refreshIndex == 0 ? '无未指派服务单...' : '无已指派服务单...'
                        });
                    }
                }
            });
        },
        _slide: function (i) {
            others.inx = i;
        },
        _lazy: function () {
            this._super();
        },
        _ready: function () {
            Vue.component('sjzpitem-template', {
                template: app.getByid(doc, 'sjzpitem-template'),
                props: {
                    model: Array
                },
                methods: {
                    showDetail: function (item) {
                        if (item.status == '1') {
                            app.openPage({
                                src: "yxsjzp/order-temp.html",
                                argsKey: 'zpordertemp_page',
                                args: {
                                    items: item,
                                    cb: function () {
                                        self._refresh();
                                    }
                                }
                            });
                        }
                    },
                    toSubmit: function (item) {
                        //item.ischecked = '1';
                        self.toSubmit(item);
                    },
                    toCancel: function (item) {
                        self.toCancel(item);
                    },
                    toDelete: function (val) {
                        self.toDelete(val);
                    },
                    statueImg: function (item) {
                        if (item.status == '1') {
                            return 'nonGive';
                        } else if (item.status == '2') {
                            return 'give';
                        } else {
                            return '';
                        }
                    },
                }
            });

            new Vue({
                el: doc.getElementById('searchs'),
                data: {
                    md: searchData
                },
                watch: {
                    'md.keyword': function () {
                        this.toSearch();
                    }
                },
                methods: {
                    toSearch: function () {
                        self._refresh();
                    }
                }
            });

            new Vue({
                el: doc.getElementById('footdiv'),
                data: others,
                methods: {
                    toChecked: function () {
                        self._getRefreshObj().data.addMap({
                            ischecked: others.ischecked
                        }, true);
                    },
                    toSubmit: function () {
                        self.toSubmit();
                    },
                    toDelete: function () {
                        self.toDelete();
                    },
                    toCancel: function () {
                        self.toCancel();
                    },
                }
            });
        }
    });

    return new Index();

});